<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-select
          v-model="queryParams.userId"
          filterable
          remote
          reserve-keyword
          placeholder="请输入用户名"
          :remote-method="remoteMethod"
          :loading="loading">
          <el-option
            v-for="item in selectUser"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="deptId" v-if="this.admin">
        <el-select v-model="queryParams.deptId"  placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dept in this.deptList"
            :key="dept.deptId"
            :label="dept.deptName"
            :value="dept.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="addOrder" >新增订单</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="订单号" align="center" prop="id" />
      <el-table-column label="用户" align="center" prop="user.nickName" />
      <el-table-column label="门店" align="center" prop="dept.deptName" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template v-slot:default="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="药包数量" align="center" prop="medicineCount" />
      <el-table-column label="药剂数量" align="center" prop="resultCount" />
      <el-table-column label="药包总重量" align="center" prop="weight" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot:default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled = "scope.row.status !== 0"
            @click="handleClose(scope.row)"
            >关闭</el-button>
          <router-link :to="'/system/order-process/index/'+ scope.row.id"  class="link-type">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              :disabled = "scope.row.status === 0"
              >详情</el-button>
          </router-link>
        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
        title="新增订单"
        size="80%"
        :visible.sync="drawer"
        :direction="direction"
        :before-close="handleDrawerClose">
      <add-order></add-order>
    </el-drawer>
  </div>
</template>

<script>
import { listOrder, getUserLikeByNickName, closeOrder } from "@/api/manage/order";
import {listDept} from "@/api/system/dept";
import AddOrder from "@/views/manage/order/addOrder.vue";
export default {
  name: "Order",
  components: {AddOrder},
  dicts: ['option_type', 'order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 订单表格数据
      selectUser:[],
      orderList: [],
      deptList:[],
      admin: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        status: null,
      },
      drawer: false,
      direction: 'ltr',
    };
  },
  created() {
    let deptId = this.$store.getters.deptId
    this.admin = this.isAdmin();

    if(!this.admin){
      this.deptId = deptId
      this.queryParams.deptId = deptId
    } else {
      this.getDetpList()
    }

    this.getList();
  },
  methods: {
    handleDrawerClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    remoteMethod(query) {
      getUserLikeByNickName({nickName:query}).then(response => {
        this.selectUser = response.data;
                console.log(response.data);
      })
    },
    getDetpList() {
      this.loading = true;
      listDept({
        status: 0
      }).then(response => {
        this.deptList = response.data;
      }).finally(()=>{
        this.loading = false;
      })
    },
    isAdmin(){
      let roles = this.$store.getters.roles;
      for(let i=0;i<roles.length;i++){
        if(roles[i] === 'admin'){
          return true;
        }
      }
      return false;
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          userId: null,
          deptId: this.deptId,
          status: null,
      }
      this.handleQuery();
    },
    addOrder(){
      this.drawer = true
    },
    /** 关闭订单 */
    handleClose(obj) {
      closeOrder(obj).then(response => {
        this.getList();
      })
    }
  }
};
</script>
